import {
  allTag,
  getTag
} from '../../utils/api'
const app = getApp()
Component({
  properties: {
    // 这里定义了innerText属性，属性值可以在组件使用时指定
  },
  data: {
    items: [],
    arr: [], //标签数组：用来存储选中的值
    allTags:[],
  },
  lifetimes: {
    attached() {
      fliterId:{
        let userId = app.globalData.userId
        allTag().then(res => {
          console.log('所有标签',res);
          let items = res.data

          this.setData({
            allTags:items
          })
          return items;
        }).then((items)=>{
          getTag(userId).then((res)=>{
            let arr2 = items
            let arr1 = res.data
            let add = arr2.filter(item => !arr1.some(ele=>ele.id==item.id));
            this.setData({
              items:add
            })
          })
        })
      }
    }
  },
  methods: {
    checkLabs(e) {
      var that = this,
        index = e.currentTarget.dataset.index,
        value = e.currentTarget.dataset.value,
        items = that.data.items,
        arr = that.data.arr,
        val = items[index].checked, //点击前的值
        limitNum = 10,
        curNum = 0; //已选择数量
      //选中累加
      for (var i in items) {
        if (items[i].checked) {
          curNum += 1;
        }
      }
      if (!val) {
        if (curNum == limitNum) {
          wx.showModal({
            content: '选择数量不能超过' + limitNum + '个',
            showCancel: false
          })
          return;
        }
        arr.push(value);
      } else {
        for (var i in arr) {
          if (arr[i] == value) {
            arr.splice(i, 1);
          }
        }
      }
      items[index].checked = !val;
      that.setData({
        items: items,
        arr: arr
      })
      this.triggerEvent("addInterest", arr)
    },
    //默认选中为true的
    // selectEd(e){
    //   var that = this,
    //     arr = that.data.arr,
    //     items = that.data.items
    //   for (var i = 0; i < items.length; i++) {
    //     if (items[i].checked) {
    //       var value = items[i].value;
    //       arr.push(value);
    //       that.setData({
    //         items: items,
    //         arr: arr
    //       })
    //     }
    //   }
    // },
  }
})